<?php
/**
 * 广告平台
 * 后台开发者管理
 *
 * @author Ciro <ciro.han@shangbox.com>
 * @copyright 2011-2012 shangbox.com
 * $Id: App.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @history 2011-2012  ::  Ciro  ::  Create File
 */

class Model_App extends Model_Abstract
{

    protected $db;

    function __construct()
    {
        $this->db = Com_DB::get('BingoMobi');
        $this->_session = Core_Session::getInstance();
    }
    /**
     * 获取APP信息
     * @param array $seach
     * @param int $star
     * @param int $estar
     * @param int $type
     * @return array
     */
    function _getAppList($seach, $star, $estar, $wheresql,$seachtype,$type = 0)
    {
        switch ($seachtype){
            case "1":  //应用首页搜索
                $sql=" AND 1 ";
                if(!empty($wheresql["name"])){
                    $sql=" AND name LIKE '%".$wheresql["name"]."%' ";
                }
                if(!empty($wheresql["type"])){
                    $sql=" AND appactive='{$wheresql["type"]}' ";
                }
                break;
            case "2":   //上传应用搜索
                $sql=" AND appactive in({$wheresql})";
                break;
            case "3":   //报表搜索
                $sql=" ";
                break;
            default:
                $sql=" ";
        }
        $count = $this->db->fetchRow("SELECT count(*) AS sum FROM bg_app_info WHERE 1{$sql}AND active={$type}");
        $lis = $this->db->limitQuery("SELECT * FROM bg_app_info WHERE 1{$sql}AND active={$type}", $star, $estar);
        $list = array($count["sum"], $lis);
        return $list;
    }
    //put your code here
      /**
     * APP信息获取
     * @param type $id
     * @return type
     */
    function _getApp($id)
    {
        $row = $this->db->fetchRow("SELECT * FROM bg_app_info WHERE id='" . $id . "'");
        //应用详细读取
        if ($row["id"] > 0) {
            $list = $this->db->fetchAll("SELECT * FROM bg_app_content WHERE appid='{$row["id"]}'");
            //显示数据处理
            if (is_array($list)) {
                foreach ($list as $vl) {
                    $row["adinfo"][$vl["orderid"]] = $vl;
                }
            }
        }
        return $row;
    }
     /**
     * 应用第一步添加
     * @param type $uid
     * @param type $data
     * @return type
     */
    function _addApp($uid, $data)
    {
        $ok = 0;
        $key = Helper_Public::cretae_appKeySecret();
        $setArr = array(
            "uid" => "{$uid}",
            "name" => "{$data["name"]}",
            "version" => "{$data["version"]}",
            "platformid" => "{$data["platformid"]}",
            "classid" => "{$data["classid"]}",
            "appurl" => "{$data["appurl"]}",
            "appimg" => "{$data["appimg"]}",
            "introduce" => "{$data["introduce"]}",
            "appactive" => "0",
            "assid" => "{$data["assid"]}",
            "appkey" => "{$key["key"]}",
            "appsecret" => "{$key["secret"]}",
            "active" => "-1"
        );
        $ok = $this->db->insert('bg_app_info', $setArr);
        return $ok;
    }
      /**
     * 应用更新
     * @param type $id
     * @param type $data
     * @return string
     */
    function _updateApp($id, $data)
    {
        $setArr = array(
            "uid" => "{$data["uid"]}",
            "name" => "{$data["name"]}",
            "version" => "{$data["version"]}",
            "platformid" => "{$data["platformid"]}",
            "classid" => "{$data["classid"]}",
            "assid" => "{$data["assid"]}",
            "appurl" => "{$data["appurl"]}",
            "appimg" => "{$data["appimg"]}",
            "introduce" => "{$data["introduce"]}",
            "appactive" => "0",
            "active" => "0"
        );
        $ok = $this->db->update("bg_app_info", $setArr, "id={$id}");
        if ($ok) {
            return $id;
        } else {
            //更新失败
            return "";
        }
    }
     /**
     * app广告添加
     * @param type $data
     * @param type $type
     * appid orderid type fun_type one two adactive stim etim
     * appid orderid排序ID type(0游戏设计方式,1cpm,2cpa) fun_type(游戏设计方式 0-关卡，1-等级,2-分数 )
     */
    function _addsetApp($data, $type)
    {
        $appid = $this->_session["appID"];
        $ok = 0;
        //主APP信息更新
        $setArr = array("active" => "8","appactive" => "{$type}","downurl" => "{$data["downurl"]}");
        $ok = $this->db->update("bg_app_info", $setArr, "id={$appid}");
        //类型添加
        $setArr = array(
            "appid" => "{$appid}",
            "orderid" => "0",
            "type" => "0",
            "fun_type" => "{$data["0"]}",
            "adactive" => "0"
        );
        $ok = $this->db->insert('bg_app_content', $setArr);
        //cpm添加
        $setArr = array(
            "appid" => "{$appid}",
            "orderid" => "1",
            "type" => "1",
            "fun_type" => "{$data["0"]}",
            "one" => "{$data[1]["one"]}",
            "two" => "{$data[1]["two"]}",
            "adactive" => "0"
        );
        $ok = $this->db->insert('bg_app_content', $setArr);

        //cap2-8广告添加
        for ($i = 2; $i < 9; $i++) {
            $setArr = array(
                "appid" => "{$appid}",
                "orderid" => "{$i}",
                "type" => "2",
                "fun_type" => "{$data["0"]}",
                "one" => "{$data[$i]["one"]}",
                "two" => "{$data[$i]["two"]}",
                "adactive" => "0"
            );
            $ok = $this->db->insert('bg_app_content', $setArr);
        }
        // 没有全部成功 数据回滚
        return $ok;
    }
    function _delApp($id){
        $ok= $this->db->query("DELETE FROM bg_app_info WHERE id='{$id}'");
        $ok= $this->db->query("DELETE FROM bg_app_content WHERE appid='{$id}'");
        return $ok;
    }
     /**
     * 分类读取
     * @return type
     */
    function _getClass()
    {
        $list = array();
        $list = $this->db->fetchAll("SELECT * FROM bg_app_class WHERE type='0'");
        return $list;
    }
}

?>
